package com.assistant.admin.service.impl;

import com.assistant.admin.pojo.PageResult;
import com.assistant.admin.pojo.PageVariable;
import com.assistant.admin.service.UserManagerService;
import com.assistant.wechat.mapper.LoginMapper;
import com.assistant.wechat.pojo.WxUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class UserMangerServiceImpl implements UserManagerService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public PageResult<WxUser> getUserBy(PageVariable pageVariable) {
        Example example = new Example(WxUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(pageVariable.getKey())) {
            criteria.andLike("nick_name", "%" + pageVariable.getKey() + "%").orEqualTo("nick_name", pageVariable.getKey());
        }
        PageHelper.startPage(pageVariable.getPage(), pageVariable.getRows());
        List<WxUser> list = loginMapper.selectByExample(example);
        PageInfo<WxUser> brandPageInfo = new PageInfo<>(list);
        return new PageResult<WxUser>(brandPageInfo.getTotal(), brandPageInfo.getList());
    }

    @Override
    public Boolean deleteUser(Integer id) {
        return loginMapper.deleteByPrimaryKey(id) == 1;
    }

}
